package com.ubst.smartad.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.ubst.smartad.dao.TSALogCntDO;
import com.ubst.smartad.logger.Debug;

public class TSALogCntMngImpl {
	
	Connection con = null;
	private String serverName;
	private Debug debug;
	
	/** 
     * 생성자
     *   
    */ 
	public TSALogCntMngImpl(Connection con, String serverName, String log_level) {
		this.con = con;
		this.serverName = serverName;
		this.debug = new Debug("TSALogCntMngImpl", log_level);
	}
	
	/**
	 * 로그카운터 등록
	 * @param userAccountDO
	 * @throws SQLException
	 */
	public void insertLogCnt(TSALogCntDO logCntDO) throws SQLException {
		PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        
        try {
            queryBuf = new StringBuffer();
			queryBuf.append("INSERT INTO TSA_LOG_CNT " )
				.append(" ( ")
				.append("  LOG_SERIAL ,SYSTEMID ,LOGINCNT ,LOGINFAILCNT ,PASSCHGCNT ")
				.append(" ,ACCOUNTCNT ,ACCOUNTCHGCNT ,PASSEXPIRECNT ,ACCREQCNT ,ACCEXPIRECNT ")
				.append(" ,REALACCCNT ,UNUSEDACCCNT ,BLOCKACCCNT ,REALUSERCNT ,UNUSEDUSERCNT ")
				.append(" ,POLICYCHGCNT ,SYNCCNT, LOGDATE ")
				.append(" ) ")
				.append(" VALUES")
				.append(" ( ") 
				.append("  ? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ,? ,? ")
				.append("  ? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ")
				.append(" ) ");

			debug.debug("insertLogCnt ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setInt(idx++, logCntDO.log_serial);
			pstmt.setString(idx++, logCntDO.systemid);
			pstmt.setInt(idx++, logCntDO.logincnt);
			pstmt.setInt(idx++, logCntDO.loginfailcnt);
			pstmt.setInt(idx++, logCntDO.passchgcnt);
			pstmt.setInt(idx++, logCntDO.accountcnt);
			pstmt.setInt(idx++, logCntDO.accountchgcnt);
			pstmt.setInt(idx++, logCntDO.passexpirecnt);
			pstmt.setInt(idx++, logCntDO.accreqcnt);
			pstmt.setInt(idx++, logCntDO.accexpirecnt);
			pstmt.setInt(idx++, logCntDO.realacccnt);
			pstmt.setInt(idx++, logCntDO.unusedacccnt);
			pstmt.setInt(idx++, logCntDO.blockacccnt);
			pstmt.setInt(idx++, logCntDO.realusercnt);
			pstmt.setInt(idx++, logCntDO.unusedusercnt);
			pstmt.setInt(idx++, logCntDO.policychgcnt);
			pstmt.setInt(idx++, logCntDO.synccnt);
			pstmt.setString(idx++, logCntDO.logdate);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;
        } catch (SQLException e){
			throw e;
        } finally {
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }
	}
	
	/**
	 * 로그카운터 수정
	 * @param userAccountDO
	 * @throws SQLException
	 */
	public void updateLogCnt(TSALogCntDO logCntDO) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {
            queryBuf = new StringBuffer();
			queryBuf.append("UPDATE TSA_LOG_CNT " )
				.append(" SET ")
				.append("  LOG_SERIAL=? ,SYSTEMID=? ,LOGINCNT=? ,LOGINFAILCNT=? ,PASSCHGCNT=? ")
				.append(" ,ACCOUNTCNT=? ,ACCOUNTCHGCNT=? ,PASSEXPIRECNT=? ,ACCREQCNT=? ,ACCEXPIRECNT=? ")
				.append(" ,REALACCCNT=? ,UNUSEDACCCNT=? ,BLOCKACCCNT=? ,REALUSERCNT=? ,UNUSEDUSERCNT=? ")
				.append(" ,POLICYCHGCNT=? ,SYNCCNT=? ,LOGDATE=? ")
				.append(" WHERE LOG_SERIAL=? ");

			debug.debug("updateLogCnt ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setInt(idx++, logCntDO.log_serial);
			pstmt.setString(idx++, logCntDO.systemid);
			pstmt.setInt(idx++, logCntDO.logincnt);
			pstmt.setInt(idx++, logCntDO.loginfailcnt);
			pstmt.setInt(idx++, logCntDO.passchgcnt);
			pstmt.setInt(idx++, logCntDO.accountcnt);
			pstmt.setInt(idx++, logCntDO.accountchgcnt);
			pstmt.setInt(idx++, logCntDO.passexpirecnt);
			pstmt.setInt(idx++, logCntDO.accreqcnt);
			pstmt.setInt(idx++, logCntDO.unusedusercnt);
			pstmt.setInt(idx++, logCntDO.realacccnt);
			pstmt.setInt(idx++, logCntDO.unusedacccnt);
			pstmt.setInt(idx++, logCntDO.blockacccnt);
			pstmt.setInt(idx++, logCntDO.realusercnt);
			pstmt.setInt(idx++, logCntDO.unusedusercnt);
			pstmt.setInt(idx++, logCntDO.policychgcnt);
			pstmt.setInt(idx++, logCntDO.synccnt);
			pstmt.setString(idx++, logCntDO.logdate);
			pstmt.setInt(idx++, logCntDO.log_serial);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }
	
	/**
	 * 로그카운터 목록 조회
	 * @param userid
	 * @return
	 * @throws SQLException
	 */
	public TSALogCntDO selectLogCnt(int log_serial) throws SQLException {

		PreparedStatement pstmt=null;
		ResultSet rs = null;

		TSALogCntDO logCntDO = null;

		try {
			
			StringBuffer queryBuf = new StringBuffer();
			queryBuf.append("SELECT ")
				.append("  LOG_SERIAL ,SYSTEMID ,LOGINCNT ,LOGINFAILCNT ,PASSCHGCNT ")
				.append(" ,ACCOUNTCNT ,ACCOUNTCHGCNT ,PASSEXPIRECNT ,ACCREQCNT ,ACCEXPIRECNT ")
				.append(" ,REALACCCNT ,UNUSEDACCCNT ,BLOCKACCCNT ,REALUSERCNT ,UNUSEDUSERCNT ")
				.append(" ,POLICYCHGCNT ,SYNCCNT, LOGDATE ")
				.append(" FROM TSA_LOG_CNT ")
				.append(" WHERE LOG_SERIAL=? ");

			debug.debug("selectLogCntList ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setInt(idx++, log_serial);

			rs = pstmt.executeQuery();

			if ( rs.next() ){
				logCntDO = new TSALogCntDO();
				logCntDO.log_serial = log_serial;
				logCntDO.systemid = rs.getString("SYSTEMID");
				logCntDO.logincnt = rs.getInt("LOGINCNT");
				logCntDO.loginfailcnt = rs.getInt("LOGINFAILCNT");
				logCntDO.passchgcnt = rs.getInt("PASSCHGCNT");
				logCntDO.accountcnt = rs.getInt("ACCOUNTCNT");
				logCntDO.accountchgcnt = rs.getInt("ACCOUNTCHGCNT");
				logCntDO.passexpirecnt = rs.getInt("PASSEXPIRECNT");
				logCntDO.accreqcnt = rs.getInt("ACCREQCNT");
				logCntDO.accexpirecnt = rs.getInt("ACCEXPIRECNT");
				logCntDO.realacccnt = rs.getInt("REALACCCNT");
				logCntDO.unusedacccnt = rs.getInt("UNUSEDACCCNT");
				logCntDO.blockacccnt = rs.getInt("BLOCKACCCNT");
				logCntDO.realusercnt = rs.getInt("REALUSERCNT");
				logCntDO.unusedusercnt = rs.getInt("UNUSEDUSERCNT");
				logCntDO.policychgcnt = rs.getInt("POLICYCHGCNT");
				logCntDO.synccnt = rs.getInt("SYNCCNT");
				logCntDO.logdate = rs.getString("LOGDATE");
			}

			rs.close();
			pstmt.close();

		}catch (SQLException e){
			throw e;
		}finally{
			if ( rs != null) try{rs.close();}catch(SQLException ee){}
			if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
		}

		return logCntDO;
	}
	
	/**
	 * 로그카운터 삭제
	 * @param userid
	 * @throws SQLException
	 */
	public void deleteLogCnt(int log_serial) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

            queryBuf = new StringBuffer();
			queryBuf.append("DELETE FROM TSA_LOG_CNT " )
				.append(" WHERE LOG_SERIAL=? ");

			debug.debug("deleteLogCnt ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setInt(idx++, log_serial);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }
}
